package com.mezz.bones.framework.outbox.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mezz.bones.framework.outbox.domain.entity.OutboxMessage;
import com.mezz.bones.framework.outbox.enums.MessageStatusEnum;
import com.mezz.bones.framework.outbox.mapper.IOutboxMessageMapper;
import com.mezz.bones.framework.outbox.repository.IOutboxMessageRepository;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 事务消息表 服务实现类
 * </p>
 *
 * @author mezz
 * @since 2025-04-29
 */
@Service
public class OutboxMessageRepositoryImpl extends ServiceImpl<IOutboxMessageMapper, OutboxMessage> implements IOutboxMessageRepository {

    @Override
    public List<OutboxMessage> findMessagesToArchive(Date archiveDate, Integer limit) {

        LambdaQueryWrapper<OutboxMessage> q = new LambdaQueryWrapper<>();
        q.ge(OutboxMessage::getCreateTime,archiveDate);
        q.in(OutboxMessage::getStatus, MessageStatusEnum.COMPLETED.getCode(),MessageStatusEnum.CANCELLED.getCode());
        q.last("limit " + limit);

        return list(q);
    }
}
